<?php
// +----------------------------------------------------------------------
// | CoreThink [ Simple Efficient Excellent ]
// +----------------------------------------------------------------------
// | Copyright (c) 2014 http://www.corethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com> <http://www.corethink.cn>
// +----------------------------------------------------------------------
namespace Admin\Controller;
use Think\Controller;


class AdsController extends AdminController
{
	public function index()
	{
		//接收搜索关键字
		$keyword=I("keyword");
		if(!empty($keyword))
		{
			$map['adName']=$keyword;
		}
		$keyselect=I("keyselect");
		if(!empty($keyselect))
		{
			$map['adPositionId']=$keyselect;
		}
		
		$list=D("Ads")->page(!empty($_GET['p'])?$_GET['p']:1,C("ADMIN_PAGE_ROWS"))->where($map)->order("adId desc")->select();
		
		$page=new \Common\Util\Page(M("ads")->where($map)->count(),C("ADMIN_PAGE_ROWS"));
		
		$builder = new \Common\Builder\ListBuilder();
		$builder->_table_data_list_key='adId';
		$builder->setMetaTitle("广告列表") //设置页面标题
		->addTopButton('addnew') //添加新增按钮
		->addTopButton('resume')
		->addTopButton('forbid')
		->addTopButton('delete')
		->setSearch('请输入广告名称', U('index'),$cats_list)
		->addTableColumn('adId', 'ID')
		->addTableColumn('adName', '广告标题')
		->addTableColumn('adURL', '广告网址')
		->addTableColumn('adFile', '图标','image')
		->addTableColumn('status', '状态','status')
		->addTableColumn('right_button', '操作', 'btn')
		->setTableDataList($list) //数据列表
		->setTableDataPage($page->show())  //数据列表分页
		->addRightButton('self',array('title'=>'编辑','class'=>'label label-primary','href'=>U('Admin/Ads/edit',array('adId'=>'__data_id__'))))   //添加编辑按钮
        ->addRightButton('forbid') //添加禁用/启用按钮
        ->addRightButton('delete') //添加删除按钮	
		->display();
	}
	public function add()
	{
		if(IS_POST)
		{
			
			$data['adName']=$_POST['adName'];
			$data['adURL']=$_POST['adURL'];
			$data['adFile']=$_POST['adFile'];
			$data['adSort']=$_POST['adSort'];
			$data['status']=1;
			
			 $ad_object = D('Ads');
			$data = $ad_object->create($data);
			if($data){
				$id = $ad_object->add();
				 
				if($id){
					$this->success('新增成功', U('index'));
				}else{
					$this->error('新增失败');
				}
			}else{
				$this->error($ad_object->getError());
			} 
			
		}else{
			
			$builder = new \Common\Builder\FormBuilder();
			$builder->setMetaTitle('新增')  //设置页面标题
			->setPostUrl(U('add')) //设置表单提交地址
			->addFormItem('adName', 'text', '广告标题', '')
			->addFormItem('adURL', 'text', '广告网址', '')
			->addFormItem('adFile', 'picture', '图标','首页轮播广告1920*497')
			->addFormItem('adSort','text','排序','')
			->display();
		}
	}

	 public function edit()
	{
		
		$id=I("adId");  
		if(IS_POST)
		{
			$data['adId']=$_POST['adId'];
			
			$data['adName']=$_POST['adName'];
			$data['adURL']=$_POST['adURL'];
			$data['adFile']=$_POST['adFile'];
			$data['adSort']=$_POST['adSort'];
			$data['status']=1;
				
			$ad_object = D('Ads');
			$data = $ad_object->create($data);
			if($data){
				$id = $ad_object->save();
				 
				if($id){
					$this->success('修改成功', U('index'));
				}else{
					$this->error('修改失败');
				}
			}else{
				$this->error($ad_object->getError());
			} 
			
		}else{
			if($id)
			{
				$list=D("Ads")->find($id); 
				
				$builder = new \Common\Builder\FormBuilder();
				$builder->setMetaTitle('编辑')  //设置页面标题
				->setPostUrl(U('edit')) //设置表单提交地址
				->addFormItem('adId','hidden','ID','',$id)
				->addFormItem('adName', 'text', '广告标题', '')
				->addFormItem('adURL', 'text', '广告网址', '')
				->addFormItem('adFile', 'picture', '图标','首页轮播广告1920*497')
				->addFormItem('adSort','text', '排序','')
				->setFormData($list)
				->display();
			}
		}
	} 
	
}